#include "map.hpp"

using namespace STL;

void test_map_basic_operations() {
    std::cout << "=== map基础操作测试 ===" << std::endl;
    
    STL::map<int, std::string> myMap;
    
    // 测试插入
    assert(myMap.insert(1, "one") == true);
    assert(myMap.insert(2, "two") == true);
    assert(myMap.insert(3, "three") == true);
    assert(myMap.insert(4, "four") == true);
    
    // 测试重复插入
    assert(myMap.insert(1, "one_again") == false);
    
    std::cout << "插入测试通过" << std::endl;
    
    // 测试中序遍历
    std::cout << "map中序遍历: ";
    myMap.inorder();
}

void test_map_iterator() {
    std::cout << "\n=== map迭代器测试 ===" << std::endl;
    
    STL::map<int, std::string> myMap;
    
    // 插入测试数据
    myMap.insert(5, "five");
    myMap.insert(3, "three");
    myMap.insert(7, "seven");
    myMap.insert(1, "one");
    myMap.insert(4, "four");
    myMap.insert(6, "six");
    myMap.insert(8, "eight");
    
    std::cout << "map中序遍历: ";
    myMap.inorder();
    
    std::cout << "迭代器遍历: " << std::endl;
    auto it = myMap.begin();
    while (it != myMap.end()) {
        std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
        ++it;
    }
}

void test_map_string_keys() {
    std::cout << "\n=== 字符串键map测试 ===" << std::endl;
    
    STL::map<std::string, int> wordCount;
    
    // 插入单词计数
    wordCount.insert("apple", 5);
    wordCount.insert("banana", 3);
    wordCount.insert("cherry", 7);
    wordCount.insert("date", 2);
    wordCount.insert("elderberry", 1);
    
    std::cout << "单词统计（按字母顺序）:" << std::endl;
    auto it = wordCount.begin();
    while (it != wordCount.end()) {
        std::cout << it->first << ": " << it->second << std::endl;
        ++it;
    }
}

void test_map_student_records() {
    std::cout << "\n=== 学生记录map测试 ===" << std::endl;
    
    struct StudentInfo {
        std::string name;
        int age;
        double score;
        
        StudentInfo() = default;
        StudentInfo(const std::string& n, int a, double s) : name(n), age(a), score(s) {}
    };
    
    STL::map<int, StudentInfo> studentDB; // 学号 -> 学生信息
    
    // 插入学生记录
    studentDB.insert(1001, StudentInfo("Alice", 20, 95.5));
    studentDB.insert(1003, StudentInfo("Bob", 21, 88.0));
    studentDB.insert(1002, StudentInfo("Charlie", 22, 92.5));
    studentDB.insert(1005, StudentInfo("David", 19, 76.5));
    studentDB.insert(1004, StudentInfo("Eve", 20, 89.5));
    
    std::cout << "学生数据库（按学号排序）:" << std::endl;
    auto it = studentDB.begin();
    while (it != studentDB.end()) {
        std::cout << "学号: " << it->first 
                  << ", 姓名: " << it->second.name
                  << ", 年龄: " << it->second.age
                  << ", 成绩: " << it->second.score << std::endl;
        ++it;
    }
}

void test_map_large_dataset() {
    std::cout << "\n=== 大数据集map测试 ===" << std::endl;
    
    STL::map<int, std::string> largeMap;
    
    const int NUM_ELEMENTS = 100;
    
    // 插入大量数据
    for (int i = 0; i < NUM_ELEMENTS; ++i) {
        largeMap.insert(i, "value_" + std::to_string(i));
    }
    
    // 验证数据有序
    std::cout << "验证前20个元素:" << std::endl;
    auto it = largeMap.begin();
    int count = 0;
    while (it != largeMap.end() && count < 20) {
        std::cout << it->first << ": " << it->second << std::endl;
        assert(it->first == count);
        ++it;
        ++count;
    }
    
    std::cout << "大数据集测试通过，验证了 " << count << " 个元素" << std::endl;
}

void test_map_edge_cases() {
    std::cout << "\n=== map边界情况测试 ===" << std::endl;
    
    STL::map<int, std::string> emptyMap;
    
    // 测试空map
    assert(emptyMap.begin() == emptyMap.end());
    std::cout << "空map测试通过" << std::endl;
    
    // 测试单个元素
    STL::map<int, std::string> singleMap;
    singleMap.insert(42, "the answer");
    
    auto it = singleMap.begin();
    assert(it->first == 42);
    assert(it->second == "the answer");
    ++it;
    assert(it == singleMap.end());
    std::cout << "单元素map测试通过" << std::endl;
}

void test_map_pair_insert() {
    std::cout << "\n=== pair插入测试 ===" << std::endl;
    
    STL::map<int, std::string> myMap;
    
    // 使用pair插入
    myMap.insert(std::make_pair(10, "ten"));
    myMap.insert(std::make_pair(20, "twenty"));
    myMap.insert(std::make_pair(30, "thirty"));
    
    std::cout << "使用pair插入的结果:" << std::endl;
    auto it = myMap.begin();
    while (it != myMap.end()) {
        std::cout << it->first << ": " << it->second << std::endl;
        ++it;
    }
}

int main() {
    try {
        test_map_basic_operations();
        test_map_iterator();
        test_map_string_keys();
        test_map_student_records();
        test_map_large_dataset();
        test_map_edge_cases();
        test_map_pair_insert();
        
        std::cout << "\n🎉 所有map测试通过！" << std::endl;
        return 0;
    } catch (const std::exception& e) {
        std::cerr << "测试失败: " << e.what() << std::endl;
        return 1;
    }
}